package com.probox.modules.controller.platfom;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.probox.common.core.exception.BaseException;
import com.probox.common.core.exception.CustomException;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.core.web.page.TableDataInfo;
import com.probox.common.entity.domain.platfom.PlatformInfo;
import com.probox.common.entity.service.common.SysReginService;
import com.probox.common.entity.service.platfom.IPlatformInfoService;
import com.probox.common.entity.service.platfom.IPlatformStoreService;
import com.probox.common.log.annotation.Log;
import com.probox.common.log.enums.BusinessType;
import com.probox.common.security.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 平台信息Controller
 *
 * @date 2021-06-19
 */
@RestController
@RequestMapping("/platform/info")
public class PlatformInfoController extends BaseController {
    @Autowired
    private IPlatformInfoService platformInfoService;

    @Autowired
    private SysReginService sysReginService;

    @Autowired
    private IPlatformStoreService platformStoreService;

    @GetMapping("/getMyInfo")
    public AjaxResult getMyInfo() {
        return AjaxResult.success(platformStoreService.getById(SecurityUtils.getStoreId()));
    }

    /**
     * 查询店铺信息列表
     *
     * @param platformInfo
     * @return
     */
    @GetMapping("/store/list")
    @PreAuthorize(hasPermi = "platform:store:list")
    public TableDataInfo listStore(PlatformInfo platformInfo) {
        startPage();
        List<PlatformInfo> list = platformInfoService.queryListStore(platformInfo);
        return getDataTable(list);
    }

    /**
     * 店铺预警列表
     *
     * @param platformInfo
     * @return
     */
    @GetMapping("/warning/list")
    @PreAuthorize(hasPermi = "platform:warning:query")
    public TableDataInfo getWarningList(PlatformInfo platformInfo) {
        platformInfo.setAuthTime(DateUtil.date());
        startPage();
        List<PlatformInfo> list = platformInfoService.queryListStore(platformInfo);
        return getDataTable(list);
    }

    /**
     * 获取店铺信息详细信息
     */
    @GetMapping(value = "/detail")
    @PreAuthorize(hasPermi = "platform:store:get")
    public AjaxResult getInfo() {
        AjaxResult ajax = AjaxResult.success();
        String[] labels = new String[3];
        PlatformInfo platformInfo = platformInfoService.selectPlatformInfoById();
        labels[0] = platformInfo.getPlatformProvince();
        labels[1] = platformInfo.getPlatformCity();
        labels[2] = platformInfo.getPlatformArea();
        Long[] reginCode = sysReginService.getReginCode(labels);
        if (reginCode != null && reginCode.length == 3) {
            ajax.put("regin", reginCode);
        } else {
            throw new CustomException("省市区填写错误,请输入正确的省市区名称如：湖南省,长沙市,岳麓区");
        }
        ajax.put("data", platformInfo);
        return ajax;
    }

    /**
     * 查询平台信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PlatformInfo platformInfo) {
        startPage();
        List<PlatformInfo> list = platformInfoService.queryList(platformInfo);
        return getDataTable(list);
    }

    /**
     * 修改平台信息
     */
    @PreAuthorize(hasPermi = "platform:store:save")
    @Log(title = "平台信息", businessType = BusinessType.UPDATE)
    @PutMapping("store/edit")
    public AjaxResult save(@RequestBody PlatformInfo platformInfo) {
        return toAjax(platformInfoService.updatePlatformInfo(platformInfo));
    }

    /**
     * 修改平台信息
     */
    @PreAuthorize(hasPermi = "platform:info:edit")
    @Log(title = "平台信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PlatformInfo platformInfo) {
        return toAjax(platformInfoService.updatePlatformInfo(platformInfo));
    }

    @PreAuthorize(hasPermi = "platform:info:edit")
    @Log(title = "平台信息", businessType = BusinessType.UPDATE)
    @PutMapping("/time")
    public AjaxResult editTime(@RequestBody PlatformInfo platformInfo) {
        return toAjax(platformInfoService.updatePlatformInfoTime(platformInfo));
    }

    /**
     * 新增平台信息
     */
    @Log(title = "平台信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PlatformInfo platformInfo) {
        return AjaxResult.success(platformInfoService.insertPlatformInfo(platformInfo));
    }


    /**
     * 删除平台信息
     */
    @Log(title = "平台信息", businessType = BusinessType.DELETE)
    @DeleteMapping()
    public AjaxResult remove(@RequestBody HashMap map) {
        if (ObjectUtil.isEmpty(map)) throw new BaseException("传参有误！");
        if (ObjectUtil.isEmpty(map.get("platformUid"))) throw new BaseException("传参有误！");
        if (ObjectUtil.isEmpty(map.get("pa"))) throw new BaseException("传参有误！");

        return toAjax(platformInfoService.deletePlatformInfoByIds(map));
    }

    /**
     * 校验token
     *
     * @return
     */
    @GetMapping("/checkToken")
    public AjaxResult checkToken() {
        return AjaxResult.success(SecurityUtils.isAdmin(SecurityUtils.getUserId()));
    }

    /**
     * 校验是否让用户创建店铺
     *
     * @return
     */
    @GetMapping("/checkAuth")
    public AjaxResult checkAuth() {
        return AjaxResult.success(platformInfoService.checkAuth(SecurityUtils.getUserId()));
    }

}
